package cn.imvc.app.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.imvc.app.entities.Bank;
import cn.imvc.app.entities.Bankcard;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.BankModel;
import cn.imvc.app.models.BankcardModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.app.models.RechargeModel;

@Controller
@RequestMapping("bankcard")
public class BankcardControllerImpl implements BankcardController {

	@Autowired
	MemberModel memberModel;

	@Autowired
	RechargeModel rechargeModel;

	@Autowired
	BankcardModel bankcardModel;
	
	@Autowired
	BankModel bankModel;



	@RequestMapping("bankcardlist.html")
	public String bankcardlist(Model model, String token) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.html";
		}
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		member = memberModel.get(member.getId());		
		model.addAttribute("bankcardList", member.getBankcard());

		return "bankcard/bankcardlist";
	}

	@RequestMapping("bankcard.html")
	public String bankcard(Model model, String token,Integer bankcardId) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.html";
		}
		List<Bank> bankList=bankModel.findAll((byte)0);
		model.addAttribute("bankList", bankList);
		
		if(bankcardId!=null){
			Bankcard bankcard=bankcardModel.get(bankcardId);
			model.addAttribute("bankcard", bankcard);
			model.addAttribute("bankId",bankcard.getBank().getId());
		}else{
			model.addAttribute("bankId",0);
		}
		
		return "bankcard/bankcard";
	}
	
	@RequestMapping("bankcard.htm")
	public String pcbankcard(Model model, String token,Integer bankcardId) {
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			return "redirect:/member/login.html";
		}
		List<Bank> bankList=bankModel.findAll((byte)0);
		model.addAttribute("bankList", bankList);
		
		if(bankcardId!=null){
			Bankcard bankcard=bankcardModel.get(bankcardId);
			model.addAttribute("bankcard", bankcard);
			model.addAttribute("bankId",bankcard.getBank().getId());
		}else{
			model.addAttribute("bankId",0);
		}
		
		return "bankcard/pcbankcard";
	}
	
	@RequestMapping("saveBankcard.json")
	@ResponseBody
	public Map<String, Object> saveBankcard(Model model, String token, Bankcard bankcard,Integer bankId) {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			resultMap.put("code", "-1");
			resultMap.put("msg", "未登录");
			return resultMap;
		}
		if("".equals(bankcard.getBranch())||bankcard.getBranch()==null){
			resultMap.put("code", "0");
			resultMap.put("msg", "请输入支行");
			return resultMap;
		}
		if("".equals(bankId)||bankId==null){
			resultMap.put("code", "0");
			resultMap.put("msg", "请选择银行");
			return resultMap;
		}
		if("".equals(bankcard.getBankCode())||bankcard.getBankCode()==null){
			resultMap.put("code", "0");
			resultMap.put("msg", "请输入银行卡号");
			return resultMap;
		}
		if("".equals(bankcard.getCardholder())||bankcard.getCardholder()==null){
			resultMap.put("code", "0");
			resultMap.put("msg", "请输入持卡人");
			return resultMap;
		}
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		member = memberModel.get(member.getId());
		
		if(bankcard.getIsDefault()==1){
			bankcardModel.cancelDefault(member);
		}
		if (member.getBankcard().size()==0){
			bankcard.setIsDefault((byte)1);
		}
		Bank bank=new Bank();
		bank.setId(bankId);
		bankcard.setMember(member);
		bankcard.setBank(bank);
		bankcardModel.save(bankcard);
		resultMap.put("code", "1");
		resultMap.put("msg", "保存成功");		
		
		return resultMap;
	}
	
	@RequestMapping("deleteBankcard.json")
	@ResponseBody
	public Map<String, Object> deleteBankcard(Model model, String token,Integer bankcardId) {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			resultMap.put("code", "-1");
			resultMap.put("msg", "未登录");
			return resultMap;
		}
		
		if (bankcardId==null){
			resultMap.put("code", "0");
			resultMap.put("msg", "请选择银行卡");
			return resultMap;
		}
		
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		
		Bankcard bankcard=bankcardModel.get(bankcardId);
		if(bankcard==null){
			resultMap.put("code", "0");
			resultMap.put("msg", "银行卡不存在");	
			return resultMap;
		}
		
		if(bankcard.getMember().getId()!=member.getId()){
			resultMap.put("code", "0");
			resultMap.put("msg", "您没有权限这么操作");	
			return resultMap;
		}
		
		bankcardModel.delete(bankcardId);
		resultMap.put("code", "1");
		resultMap.put("msg", "删除成功");		
		
		return resultMap;
	}
	
	@RequestMapping("defaultBankcard.json")
	@ResponseBody
	public Map<String, Object> defaultBankcard(Model model, String token,Integer bankcardId) {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 用户未登录状态直接跳转登陆页面
		if (token == null || !memberModel.checkLogin(token)) {
			resultMap.put("code", "-1");
			resultMap.put("msg", "未登录");
			return resultMap;
		}		
		// 通过token从缓存里取member
		Member member = memberModel.getLoginMember(token);
		member = memberModel.get(member.getId());
		
		if (bankcardId==null){
			resultMap.put("code", "0");
			resultMap.put("msg", "请选择银行卡");
			return resultMap;
		}
		
		bankcardModel.cancelDefault(member);
		
		Bankcard bankcard=bankcardModel.get(bankcardId);
		bankcard.setIsDefault((byte)1);
		bankcardModel.save(bankcard);
		
		resultMap.put("code", "1");
		resultMap.put("msg", "设置成功");				
		
		return resultMap;
	}
}
